package swing;

import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;

import swing.components.tablemodels.WeaponTraitTableModel;
import utils.XMLFileHandler;
import container.WeaponTraitDescription;
import container.WeaponTraitDescriptions;
import enums.StandartFileNames;

/**
 * Class which is used to show a Table with Wepon traits and enables to add,
 * remove, edit and save these traits.
 * 
 * @author Marcus
 * 
 */
public class WeaponTraitFrame extends JFrame implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = -57008205853397845L;
	private WeaponTraitDescriptions weaponTraits;
	private JTable weaponTraitTable;
	private WeaponTraitCreationFrame traitCreationFrame;
	private boolean changed;

	private String filename;
	
	public static final String EDIT_COMMAND = "Edit";
	public static final String CREATE_NEW_COMMAND = "Create new Trait";
	public static final String SAVE_COMMAND = "SAVE";
	public static final String LOAD_COMMAND = "LOAD";

	public WeaponTraitFrame(String nameOfFile) {
		this.changed = false;
		this.filename = nameOfFile;
		this.weaponTraits = XMLFileHandler.loadWeaponTraits(this.filename);
		this.traitCreationFrame = new WeaponTraitCreationFrame();
		this.traitCreationFrame.addActionListener(this);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setName("WeaponTraits");
		setTitle("WeaponsTraits");

		start();
	}

	public static void main(String args[]) {
		new WeaponTraitFrame("XML_Files/"
				+ StandartFileNames.WEAPON_TRAIT_FILE.getFileName());
	}

	private void start() {
		this.weaponTraitTable = new JTable(new WeaponTraitTableModel(
				this.weaponTraits));
		this.weaponTraitTable.setVisible(true);
		add(this.weaponTraitTable, BorderLayout.CENTER);

		JButton editButton = new JButton("Edit Trait");
		editButton.addActionListener(this);
		editButton.setToolTipText("Edits the seclected Weapon Trait");
		editButton.setActionCommand(EDIT_COMMAND);

		JButton createButton = new JButton("Create New Trait");
		createButton.addActionListener(this);
		createButton.setToolTipText("Creates a new Trait");
		createButton.setActionCommand(CREATE_NEW_COMMAND);
		
		JButton saveButton = new JButton("Save");
		saveButton.addActionListener(this);
		saveButton.setToolTipText("Saves the actual Trait list.");
		saveButton.setActionCommand(SAVE_COMMAND);
	
		JPanel buttonPanel = new JPanel();
		buttonPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		buttonPanel.add(editButton);
		buttonPanel.add(createButton);
		buttonPanel.add(saveButton);
		add(buttonPanel, BorderLayout.PAGE_END);

		
		this.setSize(800,800);
		setVisible(true);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		System.out.println(e.getActionCommand());

		if (e.getActionCommand().equals(EDIT_COMMAND)) {
			int selectedRow = this.weaponTraitTable.getSelectedRow();
			System.out.println("Selected row: " + selectedRow);
			if (selectedRow > -1) {
				WeaponTraitDescription selectedDescription = this.weaponTraits
				.getTraitDescriptions().get(selectedRow);
				this.traitCreationFrame.start(selectedDescription);
			}
		}

		if (e.getActionCommand().equals(CREATE_NEW_COMMAND)) {
			this.traitCreationFrame.start(null);
		}
		
		if (e.getActionCommand().equals(SAVE_COMMAND)) {
			XMLFileHandler.storeWeaponTraits(this.weaponTraits, this.filename);
		}

		if (e.getSource().equals(this.traitCreationFrame)) {
			System.out.println("WeaponTraitCreationFrame: "
					+ e.getActionCommand());
			if (e.getActionCommand().equals(WeaponTraitCreationFrame.OK)) {
				this.weaponTraits.addTraitDescription(this.traitCreationFrame
						.getTrait());
				this.changed = true;
				repaint();
			}
		}
		
		

	}

}
